Crate attohttpc

source ·
Expand description

This project’s goal is to provide a lightweight and simple HTTP client for the Rust ecosystem. The intended use is for projects that have HTTP needs where performance is not critical or when HTTP is not the main purpose of the application. Note that the project still tries to perform well and avoid allocation where possible, but stays away from Rust’s asynchronous stack to provide a crate that’s as small as possible. Features are provided behind feature flags when possible to allow users to get just what they need.

Check out the repository for more information and examples.

Quick start

let obj = json!({
    "hello": "world",
});

let resp = attohttpc::post("https://my-api.org/do/something")
    .header("X-My-Header", "foo")   // set a header for the request
    .param("qux", "baz")            // set a query parameter
    .json(&obj)?                    // set the request body (json feature required)
    .send()?;                       // send the request

// Check if the status is a 2XX code.
if resp.is_success() {
    // Consume the response body as text and print it.
    println!("{}", resp.text()?);
}

Features

  • basic-auth support for basic auth
  • charsets support for decoding more text encodings than just UTF-8
  • compress support for decompressing response bodies using miniz_oxide (default)
  • compress-zlib support for decompressing response bodies using zlib instead of miniz_oxide (see flate2 backends)
  • compress-zlib-ng support for decompressing response bodies using zlib-ng instead of miniz_oxide (see flate2 backends)
  • json support for serialization and deserialization
  • form support for url encoded forms (does not include support for multipart)
  • multipart-form support for multipart forms (does not include support for url encoding)
  • tls-native support for tls connections using the native-tls crate (default)
  • tls-native-vendored activate the vendored feature of native-tls
  • tls-rustls-webpki-roots support for TLS connections using rustls instead of native-tls with Web PKI roots
  • tls-rustls-native-roots support for TLS connections using rustls with root certificates loaded from the rustls-native-certs crate

Activating a feature

To activate a feature, specify it in your Cargo.toml file like so

attohttpc = { version = "...", features = ["json", "form", ...] }

Re-exports

pub use crate::charsets::Charset;

Modules

Contains types to describe request bodies
This module is a clean re-export of the encoding_rs crate. You can probably find the charset you need in here.
This module is a re-export of the http crate’s header module.

Structs

A type that contains all the errors that can possibly occur while accessing an HTTP server.
The Request Method (VERB)
A multipart form created using MultipartBuilder.
A builder for creating a Multipart body.
A file to be uploaded as part of a multipart form.
Represents a request that’s ready to be sent. You can inspect this object for information about the request.
Contains proxy settings and utilities to find which proxy to use for a given URL.
Utility to build ProxySettings easily.
RequestBuilder is the main way of building requests.
Allows to inspect the properties of a request before preparing it.
Response represents a response returned by a server.
The ResponseReader is used to read the body of a response.
Session is a type that can carry settings over multiple requests. The settings applied to the Session are applied to every request created from this Session.
An HTTP status code (status-code in RFC 7230 et al.).
TextReader converts bytes in a specific charset to bytes in UTF-8.

Enums

Common errors that can occur during HTTP requests.
Errors than can occur while parsing the response from the server.

Functions

Create a new RequestBuilder with the DELETE method.
Create a new RequestBuilder with the GET method.
Create a new RequestBuilder with the HEAD method.
Create a new RequestBuilder with the OPTIONS method.
Create a new RequestBuilder with the PATCH method.
Create a new RequestBuilder with the POST method.
Create a new RequestBuilder with the PUT method.
Create a new RequestBuilder with the TRACE method.

Type Definitions

Wrapper for the Result type with an Error.